home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / UniqueList.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  767 b   |  31 lines

  1. //: C20:UniqueList.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Testing list's unique() function
  7. #include <list>
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int a[] = { 1, 3, 1, 4, 1, 5, 1, 6, 1 };
  12. const int asz = sizeof a / sizeof *a;
  13.  
  14. int main() {
  15.   // For output:
  16.   ostream_iterator<int> out(cout, " ");
  17.   list<int> li(a, a + asz);
  18.   li.unique();
  19.   // Oops! No duplicates removed:
  20.   copy(li.begin(), li.end(), out);
  21.   cout << endl;
  22.   // Must sort it first:
  23.   li.sort();
  24.   copy(li.begin(), li.end(), out);
  25.   cout << endl;
  26.   // Now unique() will have an effect:
  27.   li.unique();
  28.   copy(li.begin(), li.end(), out);
  29.   cout << endl;
  30. } ///:~
  31.